package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.util.StringTrimUtil;
import com.china08.yxyapi.util.YXGetVideoInfoUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecCloudLessonCrtVideoController extends YxyApiBaseController {

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @Value("${weike.storage.upload.video}")
    String uploadBasePath;

    @Autowired
    YXGetVideoInfoUtil yXGetVideoInfoUtil;

    @ApiOperation("创建视频")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/crt/video", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public synchronized RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) throws Exception {

        String name = StringTrimUtil.trim(reqModel.name);
        String ext = reqModel.ext;
        String location = reqModel.location;
        String md5 = reqModel.md5;
        Integer size = reqModel.size;
        String userId = getProfile(request, response).getId();

        // 获取视频时长
        long duration = 0L;
        try {
            duration = yXGetVideoInfoUtil.getDuration(uploadBasePath + location) / 1000;
        } catch (Exception e) {
            System.out.println("创建视频获取时长失败");
        }

        YxyLessonsVideo video = videoRepository.findByMd5(md5);
        if (video == null) {
            video = new YxyLessonsVideo();
            video.setId(UUID.randomUUID().toString().replace("-", ""));
            video.setFileName(name);
            video.setFileSize(size);
            video.setExtension(ext);
            video.setLocation(location);
            video.setMd5(md5);
            video.setUserId(userId);
            video.setStatus(0);
            video.setDuration(Integer.parseInt(duration + ""));
            video = videoRepository.saveAndFlush(video);
        }
        RespModel respModel = new RespModel();
        respModel.videoId = video.getId();
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String name;
        public Integer size;
        public String ext;
        public String location;
        public String md5;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(name), "视频名称为空");
            Assert.isTrue(StringUtils.isNotBlank(ext), "未知的视频类型");
            Assert.isTrue(StringUtils.isNotBlank(location), "未知的视频地址");
            Assert.isTrue(StringUtils.isNotBlank(md5), "未知的视频md5");
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String videoId;
    }
}
